//
//  CJAlerter.swift
//  CJPack
//
//  Created by 陈警卫 on 2024-09-25.
//

import Foundation
import UIKit

public class CJAlerter: NSObject {
    
    /// 系统弹出框
    /// cancelTitle 传入(默认index = 0) 不传(index后推)
    /// destructiveTitle 传入(默认index = 1) 不传(index后推)
    /// otherTitles 传入(默认index累加)
    /// tfPlaceholders 输入框站位字符串数组(nil无输入框)
    /// tfComple 输入框字符串数组回调
    @objc public static func showAlert(title: String?,
                                 message: String?,
                                 preferredStyle: UIAlertController.Style = .alert,
                                 cancelTitle: String? = nil,
                                 destructiveTitle: String? = nil,
                                 otherTitles: [String] = [],
                                 handler: ((UIAlertAction, Int) -> Void)? = nil,
                                 tfPlaceholders: [String]? = nil,
                                 tfComple: (([String]) -> (Void))? = nil) {
        
        var actions: [String] = []
        
        if let cancelTitle = cancelTitle {
            actions.append(cancelTitle)
        }
        if let destructiveTitle = destructiveTitle {
            actions.append(destructiveTitle)
        }
        actions.append(contentsOf: otherTitles)
        
        let alertController = UIAlertController(title: title, message: message, preferredStyle: preferredStyle)
        
        if let tfPlaceholders = tfPlaceholders {
            for placeholder in tfPlaceholders {
                alertController.addTextField { tf in
                    tf.placeholder = placeholder;
                }
            }
        }
        
        for (index, actionTitle) in actions.enumerated() {
            
            let style: UIAlertAction.Style = {
                switch actionTitle {
                case cancelTitle ?? "":
                    return .cancel
                case destructiveTitle ?? "":
                    return .destructive
                default:
                    return .default
                }
            }()
            
            let action = UIAlertAction(title: actionTitle, style: style) { action in
                handler?(action, index)
                
                if tfPlaceholders != nil, let textFields = alertController.textFields {
                    var values: [String] = []
                    for tf in textFields {
                        values.append(tf.text ?? "")
                    }
                    tfComple?(values)
                }
            }
            alertController.addAction(action)
        }
        
        if let vc = CJScreen.currentVC() {
            vc.present(alertController, animated: true, completion: nil)
        }
    }
}

public extension CJAlerter {
     
    /// 系统弹出框
    /// cancelTitle 传入(默认index = 0) 不传(index后推)
    /// destructiveTitle 传入(默认index = 1) 不传(index后推)
    /// otherTitles 传入(默认index累加)
    @objc static func showAlert(title: String?,
                                message: String?,
                                preferredStyle: UIAlertController.Style = .alert,
                                cancelTitle: String? = nil,
                                destructiveTitle: String? = nil,
                                otherTitles: [String] = [],
                                handler: ((UIAlertAction, Int) -> Void)? = nil,) {
        showAlert(title: title,
                  message: message,
                  preferredStyle: preferredStyle,
                  cancelTitle: cancelTitle,
                  destructiveTitle: destructiveTitle,
                  otherTitles: otherTitles,
                  handler: handler,
                  tfPlaceholders: nil,
                  tfComple: nil)
    }
    
    /// 系统弹出框
    /// cancelTitle 传入(默认index = 0) 不传(index后推)
    /// destructiveTitle 传入(默认index = 1) 不传(index后推)
    /// otherTitles 传入(默认index累加)
    @objc static func showAlert(title: String?,
                                message: String?,
                                cancelTitle: String? = nil,
                                destructiveTitle: String? = nil,
                                otherTitles: [String] = [],
                                handler: ((UIAlertAction, Int) -> Void)? = nil,) {
        showAlert(title: title,
                  message: message,
                  preferredStyle: .alert,
                  cancelTitle: cancelTitle,
                  destructiveTitle: destructiveTitle,
                  otherTitles: otherTitles,
                  handler: handler,
                  tfPlaceholders: nil,
                  tfComple: nil)
    }
    
    /// 系统弹出框
    /// cancelTitle 传入(默认index = 0) 不传(index后推)
    /// destructiveTitle 传入(默认index = 1) 不传(index后推)
    /// otherTitles 传入(默认index累加)
    @objc static func showSheet(title: String?,
                                message: String?,
                                cancelTitle: String? = nil,
                                destructiveTitle: String? = nil,
                                otherTitles: [String] = [],
                                handler: ((UIAlertAction, Int) -> Void)? = nil,) {
        showAlert(title: title,
                  message: message,
                  preferredStyle: .actionSheet,
                  cancelTitle: cancelTitle,
                  destructiveTitle: destructiveTitle,
                  otherTitles: otherTitles,
                  handler: handler,
                  tfPlaceholders: nil,
                  tfComple: nil)
    }
}

